﻿using Microsoft.AspNetCore.Mvc;
using ManageSystem.Model;
using System.Threading.Tasks;
using System;
using ManageSystem.Service;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;

namespace ManageSystem.Controllers
{
    /// <summary>
    /// ADD and manage incomestream
    /// </summary>
    [Route("ManageSystem/[controller]/[action]")]
    [ApiController]
    [Produces("application/json")]
    public class IncomeStreamController : ControllerBase
    {
        // service
        public ProjectService projectService = new();
        public IncomeStreamService incomeStreamService = new();

        ///<summary>
        ///添加某个项目的收入流水
        ///</summary>
        ///<param name="incomeStream"></param>
        ///<returns></returns>
        [HttpPost]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> AddIncomeStream(IncomeStream incomeStream)
        {


            // 插入项目的收入流水，并且返回处理以后的项目让service插入到数据库
            try
            {
                // 当前项目
                Project projectToBeUpdated = await projectService.QueryByPrimaryKey(incomeStream.projectGuid);

                if (projectToBeUpdated == null)
                {
                    return BadRequest("流水所属项目为空");
                };

                Project projectWillBeUpdated = await incomeStreamService.DealWithIncomeStreamsOfProject(incomeStream, projectToBeUpdated, "add");

                if (projectWillBeUpdated == null)
                {
                    return BadRequest("待更新项目为空！");
                }
                // 更新数据
                bool isUpdated = await projectService.Update(projectWillBeUpdated);

                if (isUpdated)
                {
                    return Ok("添加收入流水成功!已将入账信息同步更新到项目中");
                }
                else
                {
                    return BadRequest("操作失败！");
                }
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }


        }

        ///<summary>
        ///更新某个项目的收入流水
        ///</summary>
        ///<param name="incomeStream"></param>
        ///<returns></returns>
        [HttpPut]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> UpdateIncomeStream(IncomeStream incomeStream)
        {
            try
            {
                // 当前项目
                Project projectToBeUpdated = await projectService.QueryByPrimaryKey(incomeStream.projectGuid);

                // 更新项目的收入流水，并且返回处理以后的项目让service插入到数据库
                Project projectWillBeUpdated = await incomeStreamService.DealWithIncomeStreamsOfProject(incomeStream, projectToBeUpdated, "update");

                // 更新数据
                bool isUpdated = await projectService.Update(projectWillBeUpdated);

                if (isUpdated)
                {
                    return Ok("更新收入流水成功!已将入账信息同步更新到项目中");
                }
                else
                {
                    return Ok("操作失败！");
                }
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

        ///<summary>
        ///根据projectGuid查询某个项目的收入流水
        ///</summary>
        ///<param name="projectGuid"></param>
        ///<returns></returns>
        [HttpGet]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> QueryIncomeStreamByProjectGuid(string projectGuid)
        {
            List<IncomeStream> incomeStreams = await incomeStreamService.QueryIncomeStreamByProjectGuid(projectGuid);
            if (incomeStreams.Count == 0)
            {
                return NotFound("当前项目暂未创建输入流水！");
            }
            else
            {
                return Ok(incomeStreams);
            }
        }

        ///<summary>
        ///查询数据库全部收入流水
        ///</summary>
        ///<returns></returns>
        [HttpGet]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> QueryAllIncomeStream()
        {
            List<IncomeStream> incomeStreams = await incomeStreamService.QueryAll();
            if (incomeStreams.Count == 0)
            {
                return NotFound("当前数据库暂无收入流水！");
            }
            else
            {
                return Ok(incomeStreams);
            }
        }

        ///<summary>
        ///根据主键删除某个项目的收入流水
        ///</summary>
        ///<param name="streamID"></param>
        ///<returns></returns>
        [HttpDelete]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> DeleteIncomeStream(int streamID)
        {
            try
            {
                IncomeStream incomeStream = await incomeStreamService.QueryByPrimaryKey(streamID);
                // 当前项目
                Project projectToBeUpdated = await projectService.QueryByPrimaryKey(incomeStream.projectGuid);
                // 改变流水后更新的项目
                Project projectWillBeUpdated = await incomeStreamService.DealWithIncomeStreamsOfProject(incomeStream, projectToBeUpdated, "delete");
                // 更新项目到数据库
                bool isSuccess = await projectService.Update(projectWillBeUpdated);

                if (isSuccess)
                {
                    return Ok("删除收入流水成功");
                }
                else
                {
                    return NotFound("删除收入流水失败");
                }
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }
    }
}
